package com.ryskoo.catering.orderappraise.service;

import com.ryskoo.catering.orderappraise.domain.OrderAppraisePO;
import com.ryskoo.catering.orderappraise.repository.IOrderAppraiseRepository;
import com.ryskoo.catering.ordermain.domain.OrderMainPO;
import com.ryskoo.catering.ordermain.service.IOrderMainService;
import com.ryskoo.customer.customerintegral.service.ICustomerIntegralService;
import com.ryskoo.customer.vendorinfo.domain.VendorInfoPO;
import com.ryskoo.customer.vendorinfo.service.IVendorInfoService;
import com.ryskoo.framework.core.service.BasicServiceImpl;
import com.ryskoo.framework.core.utils.DateFormatHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * @author BeHappy
 */
@Service
public class OrderAppraiseServiceImpl extends BasicServiceImpl<OrderAppraisePO> implements IOrderAppraiseService {
    @Autowired
    private IOrderAppraiseRepository orderAppraiseRepository;
    
    @Autowired
    private IVendorInfoService vendorInfoService;
    
    @Autowired
    private ICustomerIntegralService customerIntegralService;
    
    @Autowired
    private IOrderMainService orderMainService;

    @PostConstruct
    public void init() {
        this.setDao(orderAppraiseRepository);
    }

	@Override
	public OrderAppraisePO audit(String state, String uuid) {
		OrderAppraisePO m = orderAppraiseRepository.getByUuid(uuid);
		if(m != null){
			m.setAuditState(state);
			m.setAuditTime(DateFormatHelper.getNowTimeStr());
			orderAppraiseRepository.update(m);
		}
		
		return m;
	}

	/**
	 * 创建评价信息，并且修改订单信息
	 */
	@Override
	public void create(OrderAppraisePO m) {
		super.create(m);
		// 修改订单信息
		OrderMainPO order = orderMainService.getByUuid(m.getOrderUuid());
		if(order != null){
			order.setAppraiseState(true);
			orderMainService.update(order);
		}
		// 评价送积分
		int sendIntegral = 0;
		VendorInfoPO vendor = vendorInfoService.getByVendorUuid(m.getVendorUuid());
		if(vendor != null){
			sendIntegral = vendor.getAppraiseIntegral();
		}
		if(sendIntegral > 0){
			customerIntegralService.addByAppraise(m.getCustomerUuid(), m.getVendorUuid(), m.getOrderNo(), sendIntegral);
		}
	}

	
	
}
